<template>
  <div>
    <div class="tools-div">
      <el-button
        type="success"
        size="small"
        @click="handleShowSavaMenuDialog(0)"
      >
        添 加
      </el-button>
    </div>

    <el-table
      :data="list"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="title" label="菜单标题" />
      <el-table-column prop="component" label="路由名称" />
      <el-table-column prop="sortValue" label="排序" />
      <el-table-column prop="status" label="状态" #default="scope">
        {{ scope.row.status == 1 ? '正常' : '停用' }}
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作" align="center" width="280" #default="scope">
        <el-button
          type="success"
          size="small"
          @click="handleShowSavaMenuDialog(scope.row.id)"
        >
          添加下级节点
        </el-button>
        <el-button
          type="primary"
          size="small"
          @click="handleShowEditMenuDialog(scope.row)"
        >
          修改
        </el-button>
        <el-button type="danger" size="small" @click="handleDelMenu(scope.row)">
          删除
        </el-button>
      </el-table-column>
    </el-table>
    <!--新增/更新菜单的对话框-->
    <div>
      <el-dialog v-model="dialogVisible" :title="dialogTitle" width="30%">
        <el-form label-width="120px">
          <el-form-item label="菜单标题">
            <el-input v-model="sysMenu.title" />
          </el-form-item>
          <el-form-item label="路由名称">
            <el-input v-model="sysMenu.component" />
          </el-form-item>
          <el-form-item label="排序">
            <el-input v-model="sysMenu.sortValue" />
          </el-form-item>
          <el-form-item label="状态">
            <el-radio-group v-model="sysMenu.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSaveOrUpdateSysMenu">
              提交
            </el-button>
            <el-button @click="dialogVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { listMenus, DeleteById, GetById, UpdateById, Save } from '@/api/sysMenu'
import { ElMessage, ElMessageBox } from 'element-plus'
// 定义表格数据模型
const list = ref([])
//定义对话框中的表单数据
const sysMenu = ref({
  status: 1,
})
//对话框
let dialogVisible = ref(false)
//对话框标题
let dialogTitle = ref('新增菜单')
// //当页面渲染时，发起异步请求加载菜单列表
onMounted(() => {
  fetchData()
})
//修改菜单按钮对话框事件函数:回显要编辑的菜单弹出对话框
const handleShowEditMenuDialog = async row => {
  dialogTitle.value = '修改菜单'
  const { code, message, data } = await GetById(row.id)
  sysMenu.value = data
  dialogVisible.value = true
}
//更新菜单
const handleUpdateSysMenu = async () => {
  await UpdateById(sysMenu.value)
  ElMessage.success('更新成功')
  fetchData()
  dialogVisible.value = false
}
//新增或更新菜单按钮的事件函数
const handleSaveOrUpdateSysMenu = () => {
  if (sysMenu.value.id) {
    //修改
    handleUpdateSysMenu()
  } else {
    //添加
    handleSaveSysMenu()
  }
}
//新增菜单提交按钮事件函数
const handleSaveSysMenu = async () => {
  const { code, message } = await Save(sysMenu.value)
  ElMessage.success('添加成功')
  dialogVisible.value = false
  fetchData()
}
//添加菜单对话框单按钮事件函数
const handleShowSavaMenuDialog = parentId => {
  sysMenu.value = {}
  dialogTitle.value = '添加菜单'
  sysMenu.value.status = 1
  sysMenu.value.parentId = parentId
  dialogVisible.value = true
}
//删除按钮事件函数
const handleDelMenu = row => {
  ElMessageBox.confirm(`您确认删除【${row.title}】吗?`, '删除菜单', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      //异步请求删除id对应的角色
      const { code, message } = await DeleteById(row.id)
      if (+code === 200) {
        ElMessage.success('删除成功')
        fetchData()
      } else {
        //有下级节点不能直接删除
        ElMessage.warning(message)
      }
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
//查询菜单列表的函数
const fetchData = async () => {
  const { data } = await listMenus()
  list.value = data
}
</script>

<style scoped>
.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
</style>
